package com.cookie.meetproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cookie.framework.helper.GlideHelper;
import com.cookie.meetproject.R;
import com.cookie.meetproject.model.AddFriendModel;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.adapter
 * 文件名:    AddFriendAdapter
 * 创建者:    cookie
 * 创建时间:   2020/1/10 20:59
 * 描述:      多Type
 */

public class AddFriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //标题
    public static final int TYPE_TITLE = 0;
    //内容
    public static final int TYPE_CONTENT = 1;

    private Context context;
    private List<AddFriendModel> list;
    private LayoutInflater inflater;
    private OnClickListener listener;

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public AddFriendAdapter(Context context, List<AddFriendModel> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            return new TitleViewHolder(inflater.inflate(R.layout.layout_search_title_item, null));
        } else if (viewType == TYPE_CONTENT) {
            return new ContentViewHolder(inflater.inflate(R.layout.layout_search_user_item, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AddFriendModel model = list.get(position);
        if (model.getType() == TYPE_TITLE) {
            ((TitleViewHolder) holder).tvTitle.setText(model.getTitle());
        } else if (model.getType() == TYPE_CONTENT) {
            GlideHelper.loadUrl(context, model.getPhoto(), ((ContentViewHolder) holder).ivPhoto);
            ((ContentViewHolder) holder).ivSex.setImageResource(model.isSex() ? R.drawable.img_boy_icon
                    : R.drawable.img_girl_icon);
            ((ContentViewHolder) holder).tvNickName.setText(model.getNickName());
            ((ContentViewHolder) holder).tvAge.setText(model.getAge() + "岁");
            ((ContentViewHolder) holder).tvDesc.setText(model.getDesc());

            if (model.isContact()) {
                ((ContentViewHolder) holder).llContactInfo.setVisibility(View.VISIBLE);
                ((ContentViewHolder) holder).tvContactName.setText(model.getContactName());
                ((ContentViewHolder) holder).tvContactPhone.setText(model.getContactPhone());
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;

        public TitleViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView ivPhoto;
        private ImageView ivSex;
        private TextView tvNickName;
        private TextView tvAge;
        private TextView tvDesc;

        private LinearLayout llContactInfo;
        private TextView tvContactName;
        private TextView tvContactPhone;

        public ContentViewHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            ivSex = itemView.findViewById(R.id.iv_sex);
            tvNickName = itemView.findViewById(R.id.tv_nickname);
            tvAge = itemView.findViewById(R.id.tv_age);
            tvDesc = itemView.findViewById(R.id.tv_desc);

            llContactInfo = itemView.findViewById(R.id.ll_contact_info);
            tvContactName = itemView.findViewById(R.id.tv_contact_name);
            tvContactPhone = itemView.findViewById(R.id.tv_contact_phone);
        }
    }

    public interface OnClickListener {
        void onClick(int position);
    }
}
